package org.hamster.multidropbox.dropbox.model
{
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
	
	import org.hamster.common.puremvc.events.ParallelEvent;
	import org.hamster.common.puremvc.events.ParallelEventHandler;
	import org.hamster.common.puremvc.proxies.BaseRemoteProxy;
	import org.hamster.dropbox.DropboxEvent;
	import org.hamster.dropbox.models.DropboxFile;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.service.DropboxMultiUploadService;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxFile;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxLinkFolderItemVO;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxLinkFoldersVO;
	import org.hamster.multidropbox.main.constants.AppStatus;
	
	public class DropboxLinkFoldersProxy extends BaseRemoteProxy
	{
		public static const NAME:String = "org.hamster.multidropbox.dropbox.model.DropboxLinkFoldersProxy";
		
		public const dropboxLinkFoldersList:ArrayCollection = new ArrayCollection();
		
		public function DropboxLinkFoldersProxy(loadingText:String=null)
		{
			super(NAME, loadingText);
			
			dropboxLinkFoldersList.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeHandler);
		}
		
		private function collectionChangeHandler(event:CollectionEvent):void
		{
			this.sendNotification(AppDropboxEvent.DROPBOX_LINK_FOLDERS_CHANGE, this.dropboxLinkFoldersList);
		}
		
		public function getByFolderName(name:String):AppDropboxLinkFoldersVO
		{
			for each (var linkFoldersVO:AppDropboxLinkFoldersVO in this.dropboxLinkFoldersList) {
				if (linkFoldersVO.folderName == name) {
					return linkFoldersVO;
				}
			}
			return null;
		}
		
		public function getLinkItem(folderName:String, clientUid:int):AppDropboxLinkFolderItemVO
		{
			var linkFoldersVO:AppDropboxLinkFoldersVO = this.getByFolderName(folderName);
			for each (var linkFolderItemVO:AppDropboxLinkFolderItemVO in linkFoldersVO.itemList) {
				if (linkFolderItemVO.clientId == clientUid) {
					return linkFolderItemVO;
				}
			}
			return null;
		}
		
		private var _folderName:String;
		private var _clientList:Array;
		private var _metaFolderList:Array;
		private var _clientIdList:Array;
		private var _linkFoldersVO:AppDropboxLinkFoldersVO;
		
		public function link(folderName:String, clientList:Array):void
		{
			_folderName = folderName;
			_clientList = clientList;
			_metaFolderList = new Array();
			_clientIdList = new Array();
			
			for each (var client:AppDropboxClient in clientList) {
				// need to fetch from server to check if the folder exists
				client.addEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
				client.addEventListener(DropboxEvent.METADATA_FAULT,  metadataFaultHandler);
				client.metadata(folderName, 0, "", false);
			}
		}
		
		private function metadataResultHandler(event:DropboxEvent):void
		{
			var client:AppDropboxClient = event.currentTarget as AppDropboxClient;
			client.removeEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
			client.removeEventListener(DropboxEvent.METADATA_FAULT,  metadataFaultHandler);
			
			_metaFolderList.push(event.resultObject);
			_clientIdList.push(client.uid);
			
			if (!_linkFoldersVO) {
				_linkFoldersVO = new AppDropboxLinkFoldersVO();
				_linkFoldersVO.folderName = _folderName;
				dropboxLinkFoldersList.addItem(_linkFoldersVO);
			}
			summaryAndSendResult();
		}
		
		private function metadataFaultHandler(event:DropboxEvent):void
		{
			var client:AppDropboxClient = event.currentTarget as AppDropboxClient;
			client.removeEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
			client.removeEventListener(DropboxEvent.METADATA_FAULT,  metadataFaultHandler);
			client.addEventListener(DropboxEvent.FILE_CREATE_FOLDER_RESULT, fileCreateFolderResultHandler);
			client.addEventListener(DropboxEvent.FILE_CREATE_FOLDER_FAULT,  fileCreateFolderFaultHandler);
			client.fileCreateFolder(_folderName);
		}
		
		private function fileCreateFolderResultHandler(event:DropboxEvent):void
		{
			var client:AppDropboxClient = event.currentTarget as AppDropboxClient;
			client.removeEventListener(DropboxEvent.FILE_CREATE_FOLDER_RESULT, fileCreateFolderResultHandler);
			client.removeEventListener(DropboxEvent.FILE_CREATE_FOLDER_FAULT,  fileCreateFolderFaultHandler);
			_metaFolderList.push(event.resultObject);
			_clientIdList.push(client.uid);
			
			if (!_linkFoldersVO) {
				_linkFoldersVO = new AppDropboxLinkFoldersVO();
				_linkFoldersVO.folderName = _folderName;
				dropboxLinkFoldersList.addItem(_linkFoldersVO);
			}
			summaryAndSendResult();
		}
		
		private function fileCreateFolderFaultHandler(event:DropboxEvent):void
		{
			var client:AppDropboxClient = event.currentTarget as AppDropboxClient;
			client.removeEventListener(DropboxEvent.FILE_CREATE_FOLDER_RESULT, fileCreateFolderResultHandler);
			client.removeEventListener(DropboxEvent.FILE_CREATE_FOLDER_FAULT,  fileCreateFolderFaultHandler);
			
			_metaFolderList.push(event.resultObject);
			_clientIdList.push(client.uid);
			summaryAndSendResult();
		}
		
		private function summaryAndSendResult():void
		{
			if (_metaFolderList.length == this._clientList.length) {
				for (var i:int =0; i < _metaFolderList.length; i++) {
					var obj:Object = _metaFolderList[i];
					var clientId:int = _clientIdList[i];
					var linkFolderItem:AppDropboxLinkFolderItemVO = new AppDropboxLinkFolderItemVO();
					linkFolderItem.clientId = clientId;
					linkFolderItem.status = obj is DropboxFile ? AppStatus.YES : AppStatus.NO;
					this._linkFoldersVO.itemList.push(linkFolderItem);
				}
				sendNotification(AppDropboxEvent.DROPBOX_LINK_FOLDERS_RESULT, _linkFoldersVO);
			}
		}

		private var linkFolderItemList:ArrayCollection;
		private var multiUploadService:DropboxMultiUploadService;
		// upload file to link folders
		public function uploadFiles(folderName:String, clientList:ArrayCollection, fileRefList:ArrayCollection):void
		{
			var theLinkFoldersVO:AppDropboxLinkFoldersVO;
			for each (var linkFoldersVO:AppDropboxLinkFoldersVO in this.dropboxLinkFoldersList) {
				if (folderName == linkFoldersVO.folderName) {
					theLinkFoldersVO = linkFoldersVO;
				}
			}
			
			linkFolderItemList = new ArrayCollection();
			var theClientList:ArrayCollection = new ArrayCollection();
			var client:AppDropboxClient;
			for each (client in clientList) {
				for each (var linkFoldersItemVO:AppDropboxLinkFolderItemVO in theLinkFoldersVO.itemList) {
					if (client.uid == linkFoldersItemVO.clientId) {
						theClientList.addItem(client);
						linkFolderItemList.addItem(linkFoldersItemVO);
						break;
					}
				}
			}
			
			
			multiUploadService = new DropboxMultiUploadService(folderName, theClientList, fileRefList, theLinkFoldersVO);
			multiUploadService.uploadFile();
		}
		

	}
}





